import request from '@/utils/system/request'

const users = [
    {
        name    : 'admin',
        password: '123456',
        token   : 'admin',
        info    : {
            name: '系统管理员'
        }
    }, {
        name    : 'editor',
        password: '123456',
        token   : 'editor',
        info    : {
            name: '编辑人员'
        }
    }, {
        name    : 'test',
        password: '123456',
        token   : 'test',
        info    : {
            name: '测试人员'
        }
    },
]

/** 登录api */
export function loginApi(data: any) {
    return new Promise((resolve, reject) => {
        const user = users.find(user => {
            return data.name === user.name && data.password === user.password
        })
        if (user) {
            resolve({
                code: 200,
                data: {
                    token: user.token,
                },
            })
        }
        else {
            resolve({
                code: 401,
                data: {},
                msg : '用户名或密码错误'
            })
        }
    })
}

/** 获取用户信息Api */
export function getInfoApi(data: any) {
    return new Promise((resolve, reject) => {
        const {token} = data

        const info = users.find(user => user.token === token)?.info || {}

        if (info) {
            resolve({
                code: 200,
                data: {
                    info: info
                },
            })
        }
        else {
            resolve({
                code: 403,
                data: {},
                msg : '无访问权限'
            })
        }
    })
}

/** 退出登录Api */
export function loginOutApi() {
    return new Promise((resolve, reject) => {
        resolve({
            code: 200,
            data: {},
            msg : 'success'
        })
    })
}

/** 获取用户信息Api */
export function passwordChange(data: object) {
    return request({
        url    : '/user/passwordChange',
        method : 'post',
        baseURL: '/mock',
        data
    })
}

/** 获取登录后需要展示的菜单 */
export function getMenuApi() {
    return request({
        url    : '/menu/list',
        method : 'post',
        baseURL: '/mock'
    })
}
